{% extends "base.html" %}
{% block content %}
{% from 'include/input_macros.html' import input, checkbox, select %}
<script src="/inc/overview.js"></script>
{% if serv %}
<style>
@media (max-width: 1280px) {
	.div-pannel {
		height: 430px !important;
	}
}
</style>
{% endif %}
<style>
.alert-danger {
	width: 250px;
	display: inline-block;
	height: 15px;
	padding-top: 0px;
	padding-bottom: 8px;
	margin-bottom: -20px !important;
}
.ui-checkboxradio-label {
	padding-bottom: 5px !important;
	padding-top: 5px !important;
}
</style>
{% if user_status == 0 %}
	{% include 'include/no_sub.html' %}
{% else %}
{% if history %}
	{% include 'include/port_scan_history.html' %}
{% elif port_scanner_stderr != '' %}
<div style="text-align: center;">
	<br />
	<h3>You have not installed Port scanner service. Read <a href="https://roxy-wi.org/services.py?service=port_scanner"
	title="Port scanner" target="_blank">here</a> how to install Port scanner service</h3>
</div>
{% else %}
	<div id="up-pannel">
	{% if servers|length == 0 %}
		{% include 'include/getstarted.html' %}
	{% endif %}
	{% for s in servers %}
		<div id="div-server-{{s.0}}" class="div-server-hapwi
			{%- if port_scanner == 'active' %}
				div-server-head-up
			{%- else %}
				div-server-head-down
			{%- endif -%}">
			<div class="server-name">
				<input type="hidden" id="server-name-{{s.0}}" value="{{s.1}}" />
					{% for p in port_scanner_settings %}
					{% if port_scanner_settings|length > 0 %}
						{% if p.server_id == s.0 and p.enabled == 1 %}
							{% if port_scanner == 'active' %}
								<span id="portscanner_enable_status-{{p.server_id}}" class="serverUp server-status" title="Port scanner is enabled and service is UP"></span>
							{% else %}
								<span id="portscanner_enable_status-{{p.server_id}}" class="serverDown server-status" title="Port scanner is enabled, but service is DOWN"></span>
							{% endif %}
						{% elif p.server_id == s.0 and p.enabled == 0 %}
							<span id="portscanner_enable_status-{{p.server_id}}" class="serverNone server-status" title="Port scanner is disabled"></span>
						{% endif %}
					{% else %}
						<span id="portscanner_enable_status-{{p.server_id}}" class="serverNone server-status" title="Port scanner is disabled"></span>
					{% endif %}
					{% endfor %}
					{% if s.0 not in port_scanner_settings|map(attribute='server_id') %}
						<span id="portscanner_enable_status-{{s.0}}" class="serverNone server-status" title="Port scanner is disabled"></span>
					{% endif %}
					{% if not serv %}
						<a href="/app/portscanner.py?history={{s.2}}" title="View Port scanner history for {{s.1}}" style="color: #5d9ceb">{{s.1}}</a>
					{% else %}
						{{s.1}}
					{% endif %}
				{% if role <= 2 %}
					<span class="server-action"></span>
				{% endif %}
			</div>
			<div class="server-desc">
				{{s.11}}
				<br />
				IP: {{s.2}}
				<br />
				Last scan:
				{% if count_ports is defined %}
				{% for c in count_ports %}
					{% if c.0 == s.2 %}
					 {{c.1.0.1}}
					{% endif %}
				{% endfor %}
				{% endif %}
				<br />
				Total opened ports:
				{% if count_ports is defined %}
				{% for c in count_ports %}
					{% if c.0 == s.2 %}
					 {{c.1.0.0}}
					{% endif %}
				{% endfor %}
				{% endif %}
				<br />
			</div>
			<div class="server-act-links" id="server-{{s.0}}">
				{% if role <= 2 %}
					{% set portscanner_enable_id = 'portscanner_enable-' + s.0|string() %}
					{% set portscanner_notify_id = 'portscanner_notify-' + s.0|string() %}
					{% set portscanner_history_id = 'portscanner_history-' + s.0|string() %}
					{% if port_scanner_settings|length > 0 %}
						{% for p in port_scanner_settings %}
							{% if p.server_id == s.0 %}
								{% set disabled = 'false' %}
								{% if p.enabled == 0 %}
									{% set disabled = 'true' %}
								{% endif %}
								{% if p.enabled == 1 %}
									{{ checkbox(portscanner_enable_id, title="Port scanner enabled", checked='checked', desc='Port scanner') }}
								{% else %}
									{{ checkbox(portscanner_enable_id, title="Port scanner disabled", desc='Port scanner') }}
								{% endif %}
								{% if p.notify == 1 %}
									{{ checkbox(portscanner_notify_id, title="Notification via Telegram enabled", checked='checked', desc='Notify') }}
								{% else %}
									{{ checkbox(portscanner_notify_id, title="Notification via Telegram disabled", desc='Notify', disabled=disabled) }}
								{% endif %}
								{% if p.history == 1 %}
									{{ checkbox(portscanner_history_id, title="Keeping history enabled", checked='checked', desc='Keep history') }}
								{% else %}
									{{ checkbox(portscanner_history_id, title="Keeping history disabled", desc='Keep history', disabled=disabled) }}
								{% endif %}
							{% endif %}
						{% endfor %}
						{% if s.0 not in port_scanner_settings|map(attribute='server_id') %}
							{{ checkbox(portscanner_enable_id, title="Port scanner disabled", desc='Port scanner') }}
							{{ checkbox(portscanner_notify_id, title="Notification via Telegram disabled", desc='Notify', disabled='true') }}
							{{ checkbox(portscanner_history_id, title="Keeping history disabled", desc='Keep history', disabled='true') }}
						{% endif %}
					{% else %}
						{{ checkbox(portscanner_enable_id, title="Port scanner disabled", desc='Port scanner') }}
						{{ checkbox(portscanner_notify_id, title="Notification via Telegram disabled", desc='Notify', disabled='true') }}
						{{ checkbox(portscanner_history_id, title="Keeping history disabled", desc='Keep history', disabled='true') }}
					{% endif %}
				{% endif %}
				<br />
				<button onclick="scanPorts('{{s.0}}')" title="Scanning open/filtered ports for the server {{s.1}}">Run scan</button>
			</div>
		</div>
	{% endfor %}
	</div>
	<div class="add-note addName alert-info" style="width: inherit; margin-right: 15px; margin-top: 45%">
		Read about Port scanner service in this <a href="https://roxy-wi.org/services.py?service=portscanner" title="The Port scanner service description" target="_blank">article</a>
	</div>
{% endif %}
<div id="dialog-confirm" style="display: none;">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:3px 12px 20px 0;"></span>Are you sure?</p>
</div>
<div id="show_scans_ports" style="display: none; padding: 0;">
    <div id="show_scans_ports_body"></div>
</div>
<script>
	function scanPorts(id) {
		$.ajax({
			url: "options.py",
			data: {
				scan_ports: id,
				token: $('#token').val()
			},
			type: "POST",
			success: function (data) {
				data = data.replace(/\s+/g, ' ');
				if (data.indexOf('danger') != '-1' || data.indexOf('unique') != '-1' || data.indexOf('error:') != '-1') {
					toastr.error(data);
				} else {
					toastr.clear();
					$("#show_scans_ports_body").html(data);
					$("#show_scans_ports" ).dialog({
						resizable: false,
						height: "auto",
						width: 360,
						modal: true,
						title: "Opened ports",
						buttons: {
							Close: function() {
								$( this ).dialog( "close" );
								$("#show_scans_ports_body").html('');
							}
						}
					});
				}
			}
		} );
	}
	$( ".server-act-links" ).change(function() {
		var id = $(this).attr('id').split('-');
		updatePortScannerSettings(id[1])
	});
	function updatePortScannerSettings(id) {
		var portscanner_enabled = 0;
		var portscanner_notify = 0;
		var portscanner_history = 0;
		if ($('#portscanner_enable-'+id).is(':checked')) {
			portscanner_enabled = '1';
			$('#portscanner_enable_status-'+id).removeClass('serverDown');
			$('#portscanner_enable_status-'+id).removeClass('serverNone');
			$('#portscanner_enable_status-'+id).addClass('serverUp');
		}
		if (portscanner_enabled == '0') {
			$('#portscanner_enable_status-'+id).removeClass('serverDown');
			$('#portscanner_enable_status-'+id).removeClass('serverUp');
			$('#portscanner_enable_status-'+id).addClass('serverNone');
		}
		if ($('#portscanner_notify-'+id).is(':checked')) {
			portscanner_notify = '1';
		}
		if ($('#portscanner_history-'+id).is(':checked')) {
			portscanner_history = '1';
		}
		if (portscanner_enabled == '1') {
			$('#portscanner_notify-'+id).checkboxradio('enable');
			$('#portscanner_history-'+id).checkboxradio('enable');
		}
		if (portscanner_enabled == '0') {
			$('#portscanner_notify-'+id).checkboxradio('disable');
			$('#portscanner_history-'+id).checkboxradio('disable');
		}
		$.ajax( {
			url: "options.py",
			data: {
				portscanner_history_server_id: id,
				portscanner_enabled: portscanner_enabled,
				portscanner_notify: portscanner_notify,
				portscanner_history: portscanner_history,
				token: $('#token').val()
			},
			type: "POST",
			success: function( data ) {
				data = data.replace(/\s+/g,' ');
				if (data.indexOf('error:') != '-1') {
					toastr.error(data);
				} else {
					toastr.clear();
					$("#server-"+id).addClass( "update", 1000 );
					setTimeout(function() {
						$( "#server-"+id).removeClass( "update" );
					}, 2500 );
				}
			}
		} );
}
</script>
{% endif %}
{% endblock %}
